package com.solweaver.greetings.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.solweaver.greetings.dao.UserDAO;
import com.solweaver.greetings.dao.UserMetaDataDAO;
import com.solweaver.greetings.dto.GenericEnum;
import com.solweaver.greetings.dto.UserDTO;
import com.solweaver.greetings.dto.UserMetaDataDTO;
import com.solweaver.greetings.dto.UserResponse;
import com.solweaver.greetings.entity.User;
import com.solweaver.greetings.entity.UserMetaData;
import com.solweaver.greetings.entity.UserStatus;
import com.solweaver.greetings.utils.GenericUtils;

@Service
public class UserServiceImpl implements IUserService{

	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private UserMetaDataDAO userMetaDataDAO;

	@Override
	@Transactional
	public UserResponse createUserIfNotExists(UserDTO userDTO, UserMetaDataDTO userMetaDataDTO) {
		UserResponse userResponse = new UserResponse();
		User user = null;
		if(userDTO != null){
			if(userDTO.getUserId() != null){
				user = userDAO.findById(userDTO.getUserId(), false);
				if(user == null){
					GenericUtils.buildErrorDetail(userResponse, GenericEnum.INVALID_USER);
					return userResponse;
				}
			}else if(userDTO.getEmailId() != null){
				List<UserStatus> userStatusList = new ArrayList<UserStatus>();
				userStatusList.add(UserStatus.Active);
				userStatusList.add(UserStatus.InActive);
				List<User> userList = userDAO.findByEmail(userDTO.getEmailId(), userStatusList);
				if(userList != null && userList.size() > 0){
					if(userList.size() == 1){
						user = (User) userList.get(0);
						userDTO.setUserId(user.getId());
						if(user.getUserStatus().equals(UserStatus.InActive)){
							user.setUserStatus(UserStatus.Active);
							userDAO.merge(user);
							UserMetaData userMetaData = GenericUtils.convertToUserMetaDataEntity(userMetaDataDTO);
							userMetaData.setUser(user);
							userMetaDataDAO.makePersistent(userMetaData);
							user.setUserMetaData(userMetaData);
						}
					}else{
						GenericUtils.buildErrorDetail(userResponse, GenericEnum.DUPLICATE_USER);
						return userResponse;
					}
				}
			}
			if(user == null){
				try{
					user = GenericUtils.convertToUserEntity(userDTO);
					user.setUserStatus(UserStatus.Active);
					userDAO.makePersistent(user);
					userDTO.setUserId(user.getId());
					UserMetaData userMetaData = GenericUtils.convertToUserMetaDataEntity(userMetaDataDTO);
					userMetaData.setUser(user);
					userMetaDataDAO.makePersistent(userMetaData);
				}catch(Exception exception){
					exception.printStackTrace();
					GenericUtils.buildErrorDetail(userResponse, GenericEnum.DB_EXCEPTION);
					return userResponse;
				}
			}else{
				UserMetaData userMetaData = user.getUserMetaData();
				userMetaData.setLastLoggedInDeviceId(userMetaDataDTO.getLastLoggedInDeviceId());
				userMetaData.setLastLoggedInTime(userMetaData.getLastLoggedInTime());
				userMetaDataDAO.merge(userMetaData);
			}
			GenericUtils.buildErrorDetail(userResponse, GenericEnum.Success);
		}
		return userResponse;
	}
	
	
}
